#!/bin/bash
# 执行示例：sh buildImagesAndPush.sh test v6 auth-api 或 sh buildImagesAndPush.sh test v6 或 sh buildImagesAndPush.sh test
#特别注意：如果在window上编辑完，上传到服务器执行，一定记得执行set ff=unix,否则会因为编码格式导致各种执行问题
#需要提前设置好环境变量HARBOR_URL，然后在本机执行docker login
tag=$(date +%Y%m%d_%H%M%S)
projectName=api-cloud-nacos-config-starter
profile='test'
if [ $# -eq 0 ];
then
  echo "Params is need! example: sh buildImagesAndPush.sh test v6 auth-api  or sh buildImagesAndPush.sh test v6 or sh sh buildImagesAndPush.sh test"
  exit
fi

if [ $# -eq 1 ];
then
  profile=$1
fi

if [ $# -eq 2 ];
then
  profile=$1
  tag=$2
fi

if [ $# -eq 3 ];
then
  profile=$1
  tag=$2
  projectName=$3
fi

# 注意[] 字符和判断条件 之间一定要有间隔
if  [ -z $HARBOR_URL ]; then
    echo "start to build project:$projectName profile:$profile tag:$tag "
    echo "HARBOR_URL is need for environment variable,example: export HARBOR_URL=192.168.0.12:8081/weng"
    exit
fi

# 注意[] 字符和判断条件 之间一定要有间隔
echo "start to build project:$projectName profile:$profile tag:$tag "
isbuildSuccess= mvn clean assembly:assembly -P$profile
echo $isbuildSuccess
imageName=$HARBOR_URL/$projectName:$tag
docker build -t $imageName ./
docker push $imageName
docker rmi -f $imageName
echo "build success and pushed images to harbor finish! project:$projectName profile:$profile"

